package cc.fs.video.myvideoproject.ui

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import java.lang.ref.WeakReference

/**
 * Created by fostion on 2015/10/14.
 */
open class BaseActivity : AppCompatActivity() {

    val myHandler:MyHandler = MyHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        if (id == android.R.id.home) {
            finish()
            return true
        }

        return super.onOptionsItemSelected(item)
    }

    fun runDelay(runnable: ()->Unit,delayMillis:Long){
        val message = Message()
        val outer = WeakReference<()->Unit>(runnable)
        message.what = -101
        message.arg1 = -102
        message.arg2 = -103
        message.obj = outer
        myHandler.sendMessageDelayed(message,delayMillis)
    }


    class MyHandler : Handler(){
        override fun dispatchMessage(msg: Message?) {
            if(msg?.what == -101 && msg?.arg1 == -102 && msg?.arg2 == -103){
                (msg?.obj as WeakReference<()->Unit>).get()?.invoke()
                return
            }
            super.dispatchMessage(msg)
        }
    }

}